package com.badibadi.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.badibadi.infos.Chat_History_Message_Model;
import com.badibadi.infos.Results;
import com.badibadi.infos.infoUserModle;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.HtmlUtils;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;

//chat_history_item_mes
public class Chat_history_Activity extends BaseActivity {
	private Button chat_history_back, chat_history_before, chat_history_after;
	private ListView chat_history_list;
	private TextView chat_history_pagenum;
	private List<Chat_History_Message_Model> message_Models;
	private Chat_history_adapter adapter;

	private String fid;
	private int Page;
	private Results results;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(Chat_history_Activity.this);
				Utils.showMessage(Chat_history_Activity.this, getResources()
						.getString(R.string.l_net_error));
				chat_history_pagenum.setText(getResources().getString(
						R.string.l_xb42)
						+ Page + getResources().getString(R.string.ye));
				break;
			case 2:
				Utils.ExitPrgress(Chat_history_Activity.this);
				chat_history_pagenum.setText(getResources().getString(
						R.string.l_xb42)
						+ Page + getResources().getString(R.string.ye));
				adapter.notifyDataSetChanged();

				break;
			case 3:
				Utils.ExitPrgress(Chat_history_Activity.this);
				Utils.showMessage(Chat_history_Activity.this, getResources()
						.getString(R.string.l_xa10));
				chat_history_pagenum.setText(getResources().getString(
						R.string.l_xb42)
						+ Page + getResources().getString(R.string.ye));
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		goToTheTitle();
		setContentView(R.layout.chat_history_main);

		try {
			fid = getIntent().getStringExtra("fid");
			Page = 1;
		} catch (Exception e) {
			// TODO: handle exception
		}
		message_Models = new ArrayList<Chat_History_Message_Model>();
		chat_history_back = (Button) this.findViewById(R.id.chat_history_back);
		chat_history_back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Chat_history_Activity.this.finish();
			}
		});
		chat_history_before = (Button) this
				.findViewById(R.id.chat_history_before);
		chat_history_before.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (Page > 1) {
					--Page;
					Chat_lists();
				} else {
					Utils.showMessage(Chat_history_Activity.this, "已经是第一页咯");
				}
			}
		});
		chat_history_after = (Button) this
				.findViewById(R.id.chat_history_after);
		chat_history_after.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				++Page;
				Chat_lists();
			}
		});

		chat_history_pagenum = (TextView) this
				.findViewById(R.id.chat_history_pagenum);
		chat_history_list = (ListView) this
				.findViewById(R.id.chat_history_list);
		adapter = new Chat_history_adapter(Chat_history_Activity.this);
		chat_history_list.setAdapter(adapter);
		Chat_lists();
	}

	private void Chat_lists() {
		Utils.showPrgress(Chat_history_Activity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				message_Models.clear();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", Utils.getUid(Chat_history_Activity.this));
				map.put("fid", fid);
				map.put("page", Page);
				map.put("pageNum", "8");
				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "chat/lists");
				if (content != null) {
					results = Utils.checkResult_NNN(getApplicationContext(),
							content);
//					   ("历史消息返回的数据是 :" + content);
					if (results != null && !results.getRetmsg().equals("null")
							&& results.isRet()) {
						try {
							message_Models = JSONUtils.getListByJsonString(
									results.getRetmsg(),
									Chat_History_Message_Model.class);
							handler.sendEmptyMessage(2);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						handler.sendEmptyMessage(3);
						if (Page > 1) {
							--Page;
						} else {
							Page = 1;
						}
					}
				} else {
					handler.sendEmptyMessage(1);
					if (Page > 1) {
						--Page;
					} else {
						Page = 1;
					}
				}
			}
		});
	}

	private class Chat_history_adapter extends BaseAdapter {
		private Context context;

		public Chat_history_adapter(Context context) {
			this.context = context;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return message_Models.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return message_Models.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {// chat_history_item_mes
			LayoutInflater inflater = LayoutInflater.from(context);
			String a = Constants.YuyingQianZui;// 判断是否是语音
			arg1 = inflater.inflate(R.layout.chat_history_item, null);
			TextView chat_history_item_name = (TextView) arg1
					.findViewById(R.id.chat_history_item_name);
			TextView chat_history_item_time = (TextView) arg1
					.findViewById(R.id.chat_history_item_time);
			TextView chat_history_item_mes = (TextView) arg1
					.findViewById(R.id.chat_history_item_mes);
			chat_history_item_name.setText(message_Models.get(arg0)
					.getNickName());
			try {
				chat_history_item_time.setText(Utils.Get_Data(message_Models
						.get(arg0).getTime(), "yyyy-MM-dd HH:mm:ss", "000"));
			} catch (Exception e) {
				// TODO: handle exception
			}
			message_Models.get(arg0).getMsg();
			// 如果是语音
			if (message_Models.get(arg0).getMsg().startsWith(a)) {
				chat_history_item_mes.setText(getResources().getString(
						R.string.wx2_6));

			} else {
				new HtmlUtils(context, chat_history_item_mes, message_Models
						.get(arg0).getMsg()).showTextView();
			}
			// chat_history_item_mes.setText(message_Models.get(arg0).getMsg());
			return arg1;
		}

	}
}
